package com.zkthink.ceres.applet.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.common.domin.PageDate;
import com.zkthink.ceres.applet.product.domain.entity.AppletProduct;
import com.zkthink.ceres.applet.product.domain.vo.ProductCategoryVo;
import com.zkthink.ceres.applet.product.domain.vo.ProductSearchVo;
import com.zkthink.ceres.applet.product.mapper.AppletProductMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.product.mapper.CeresProductCategoryMapper;
import com.zkthink.ceres.applet.product.domain.entity.CeresProductCategory;
import com.zkthink.ceres.applet.product.service.CeresProductCategoryService;

import java.util.ArrayList;
import java.util.List;

/**

@Author cmz
@Date 2024-05-06 13:49
*/
@Service
public class CeresProductCategoryServiceImpl extends ServiceImpl<CeresProductCategoryMapper, CeresProductCategory> implements CeresProductCategoryService{
    @Resource
    private CeresProductCategoryMapper ceresProductCategoryMapper;
    @Resource
    private AppletProductMapper appletProductMapper;
    @Override
    public R findByPage(PageDate pageDate) {
        QueryWrapper<CeresProductCategory> pcProductCategoryQueryWrapper = new QueryWrapper<>();
        Page<CeresProductCategory> pcProductCategoryPage = new Page<>(pageDate.getPageNum(), pageDate.getPageSize());
        Page<CeresProductCategory> pcProductCategoryPage1 = ceresProductCategoryMapper.selectPage(pcProductCategoryPage, pcProductCategoryQueryWrapper);
        return R.success(pcProductCategoryPage1);
    }

    @Override
    public R findList(ProductSearchVo productSearchVo) {
        QueryWrapper<CeresProductCategory> pcProductCategoryQueryWrapper = new QueryWrapper<>();
        pcProductCategoryQueryWrapper.eq("is_course","1");
        pcProductCategoryQueryWrapper.eq("is_delete","0");
        pcProductCategoryQueryWrapper.eq("status","1");
        List<CeresProductCategory> ceresProductCategories = ceresProductCategoryMapper.selectList(pcProductCategoryQueryWrapper);
        List<ProductCategoryVo> list = new ArrayList<>();
        for (CeresProductCategory ceresProductCategory : ceresProductCategories) {
            ProductCategoryVo productCategoryVo = new ProductCategoryVo();
            productCategoryVo.setNavtitle(ceresProductCategory.getCategoryName());
            QueryWrapper<AppletProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_delete","0").eq("status","1").eq("is_course",1);
            queryWrapper.eq("category_id", ceresProductCategory.getId());
            if (productSearchVo.getProductName() != null) {
                queryWrapper.eq("product_name", productSearchVo.getProductName());
            }
            List<AppletProduct> appletProducts = appletProductMapper.selectList(queryWrapper);
            productCategoryVo.setShop(appletProducts);
            list.add(productCategoryVo);
        }
        return R.success(list);
    }

}
